﻿using DotNetty.Buffers;
using DotNetty.Transport.Channels.Embedded;
using EmbededChannelTest;
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace UnitTest
{
    public class UnitTester
    {
        [Fact]
        public void testFrameDecoder()
        {
            IByteBuffer buf = Unpooled.Buffer();
            for (int i = 0; i < 9; i++)
            {
                buf.WriteByte(i);
            }
            IByteBuffer input = buf.Duplicate();
            EmbeddedChannel channel = new EmbeddedChannel(new FixedLengthFrameDecoder(3));
            // 写数据
            Console.WriteLine(input.Retain());
            Assert.True(channel.WriteInbound(input.Retain()));
            Assert.True(channel.Finish());
            // 读数据
            IByteBuffer read = channel.ReadInbound<IByteBuffer>();
            Assert.Equal(buf.ReadSlice(3), read);
            read.Release();

            read = channel.ReadInbound<IByteBuffer>();
            Assert.Equal(buf.ReadSlice(3), read);
            read.Release();

            read = channel.ReadInbound<IByteBuffer>();
            Assert.Equal(buf.ReadSlice(3), read);
            read.Release();

            Assert.Null(channel.ReadInbound<object>());
            buf.Release();
        }

        [Fact]
        public void TestAbsEncoder()
        {
            IByteBuffer buf = Unpooled.Buffer();
            // 写入数据
            for (int i = 0; i < 10; i++)
            {
                buf.WriteInt(i * -1);
            }
            // 构建测试Channel
            EmbeddedChannel channel = new EmbeddedChannel(new AbsIntEncoder());
            // 测试是否能正常出站
            Assert.True(channel.WriteOutbound(buf));
            Assert.True(channel.Finish());

            for (int i = 0; i < 10; i++)
            {
                Assert.Equal(i, channel.ReadOutbound<int>());
            }
            Assert.Null(channel.ReadOutbound<object>());
        }

        [Fact]
        public void TestIntDecoder()
        {
            IByteBuffer buf = Unpooled.Buffer();
            for (int i = 0; i < 8; i++)
            {
                buf.WriteByte(i);
            }
            // 构建Channel
            EmbeddedChannel channel = new EmbeddedChannel(new ToIntDecoder());
            // 测试
            Assert.True(channel.WriteInbound(buf));
            Assert.True(channel.Finish());

            // 比如 0 1 2 3
            // 3*2^0+2*2^8+1*2^16+0*2^24
            Assert.Equal(66051, channel.ReadInbound<int>());

        }
        [Fact]
        public void TestIntDecoder2()
        {
            IByteBuffer buf = Unpooled.Buffer();
            for (int i = 0; i < 8; i++)
            {
                buf.WriteByte(i);
            }
            // 构建Channel
            EmbeddedChannel channel = new EmbeddedChannel(new ToIntDecoder2(0));
            // 测试
            Assert.True(channel.WriteInbound(buf));
            Assert.True(channel.Finish());

            // 比如 0 1 2 3
            // 3*2^0+2*2^8+1*2^16+0*2^24
            Assert.Equal(66051, channel.ReadInbound<int>());

        }
    }
}
